package com.yzt.message.conversation;

import static com.yzt.message.MessageConstants.TYPE_ACCOUNT_NOTICE_FENS;

import android.net.Uri;
import android.os.Parcel;
import android.text.TextUtils;

import com.yzt.zhuju.event.RxBus;
import com.yzt.zhuju.event.im.RongYunMessageChangeEvent;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.user.BasicUserInfoResponse;
import com.yzt.message.MessageConstants;
import com.yzt.message.util.MessageInfoUtil;
import com.yzt.plugin_imageloader.ImageLoadUtil;

import java.util.List;

import io.rong.imkit.RongIM;
import io.rong.imkit.conversationlist.model.BaseUiConversation;
import io.rong.imkit.userinfo.RongUserInfoManager;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.MessageContent;
import io.rong.imlib.model.UserInfo;

/**
 * 处理系统消息数据，只处理数据，不涉及数据排列
 */
public class SystemConversationReceiptHandler implements IReceiptHandler {

    @Override
    public List<BaseUiConversation> handleReceipt(List<BaseUiConversation> uiConversations) {

        if (uiConversations == null) {
            return null;
        }

        for (BaseUiConversation mUiConversation : uiConversations) {

            Conversation mConversation = mUiConversation.mCore;
            //过滤家族和家族小助手消息
            if (mConversation.getConversationType() == Conversation.ConversationType.GROUP) {
                uiConversations.remove(mUiConversation);
                continue;
            }
            String senderUserId = mConversation.getSenderUserId();

            try {
                //处理发送者id为空的情况
                if (TextUtils.isEmpty(senderUserId)) {

                    if (!TextUtils.isEmpty(mConversation.getTargetId())) {
                        if (mConversation.getTargetId().equals(MessageConstants.TYPE_ACCOUNT_BACKGROUND)) {
                            uiConversations.remove(mUiConversation);
                            continue;
                        }
                        UserInfo userInfo = RongUserInfoManager.getInstance().getUserInfo(mConversation.getTargetId());
                        if (userInfo != null) {
                            MessageContent messageContent = new MessageContent() {
                                @Override
                                public byte[] encode() {
                                    return new byte[0];
                                }

                                @Override
                                public int describeContents() {
                                    return 0;
                                }

                                @Override
                                public void writeToParcel(Parcel dest, int flags) {

                                }
                            };
                            messageContent.setUserInfo(userInfo);
                            mConversation.setLatestMessage(messageContent);
                            mUiConversation.onConversationUpdate(mConversation);
                        } else {
                            if (MessageInfoUtil.isNeedFilter(mConversation.getTargetId())) {
                                break;
                            }

                            // 获取用户基本信息
                            BizLayer.getInstance().getAccountModule().getBasicUserInfoRequest(mConversation.getTargetId(), new AresSubscriber<BasicUserInfoResponse>() {
                                @Override
                                protected void onSuccess(BasicUserInfoResponse mBasicUserInfoResponse) {
                                    UserInfo tempUserInfo = new UserInfo(mConversation.getTargetId(), mBasicUserInfoResponse.getUserName() != null ? mBasicUserInfoResponse.getUserName() : "", Uri.parse(ImageLoadUtil.url2webp(mBasicUserInfoResponse.getPortraitUrl(), 150)));
                                    RongIM.getInstance().refreshUserInfoCache(tempUserInfo);
                                    //刷新列表
                                    RxBus.getDefault().post(new RongYunMessageChangeEvent());
                                }
                            });
                            break;
                        }
                    }
                    continue;
                }
            } catch (Exception numberFormatException) {
                continue;
            }

            try {
                //排除系统后台消息
                if (Integer.parseInt(senderUserId) >= Integer.parseInt(TYPE_ACCOUNT_NOTICE_FENS) && senderUserId.equals(
                        MessageConstants.TYPE_ACCOUNT_BACKGROUND)) {
                    uiConversations.remove(mUiConversation);
                }
            } catch (NumberFormatException exception) {
            }
        }

        return uiConversations;
    }
}
